﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
using HF.Framework.DataClientDBAgent;

namespace HF.WorkFlow.Navigate
{
    /// <summary>
    /// 数据访问类FrmworkTreeData。
    /// </summary>
    public class FrmworkTreeData
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "TreeId", "FatherId", "Caption", "Description", "NodeType", "TreeLevel", "FullPosition", "IsVisable", "DllFileName", "DllClassName", "DllMethodName", "FormName", "OpenWindowType", "MouseIsClick", "ImageIndex" };
        private string tableName = "HF_FrmworkTree";
        private string keyField = "TreeId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(FrmworkTreeEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@TreeId", entity.TreeId, typeof(string));
            sqlDataItem.AppendParameter("@FatherId", entity.FatherId, typeof(string));
            sqlDataItem.AppendParameter("@Caption", entity.Caption, typeof(string));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
            sqlDataItem.AppendParameter("@NodeType", entity.NodeType, typeof(string));
            sqlDataItem.AppendParameter("@TreeLevel", entity.TreeLevel, typeof(int));
            sqlDataItem.AppendParameter("@FullPosition", entity.FullPosition, typeof(string));
            sqlDataItem.AppendParameter("@IsVisable", entity.IsVisable, typeof(bool));
            sqlDataItem.AppendParameter("@DllFileName", entity.DllFileName, typeof(string));
            sqlDataItem.AppendParameter("@DllClassName", entity.DllClassName, typeof(string));
            sqlDataItem.AppendParameter("@DllMethodName", entity.DllMethodName, typeof(string));
            sqlDataItem.AppendParameter("@FormName", entity.FormName, typeof(string));
            sqlDataItem.AppendParameter("@OpenWindowType", entity.OpenWindowType, typeof(bool));
            sqlDataItem.AppendParameter("@MouseIsClick", entity.MouseIsClick, typeof(bool));
            sqlDataItem.AppendParameter("@ImageIndex", entity.ImageIndex, typeof(int));

        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public FrmworkTreeEntity GetFrmworkTreeEntity(DataRow dr)
        {
            FrmworkTreeEntity entity = new FrmworkTreeEntity();
            string value = "";
            entity.TreeId = dr["TreeId"].ToString();
            entity.FatherId = dr["FatherId"].ToString();
            entity.Caption = dr["Caption"].ToString();
            entity.Description = dr["Description"].ToString();
            entity.NodeType = dr["NodeType"].ToString();
            value = dr["TreeLevel"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.TreeLevel = Convert.ToInt32(value);
            }
            entity.FullPosition = dr["FullPosition"].ToString();
            value = dr["IsVisable"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.IsVisable = true; 
                }
                else
                {
                    entity.IsVisable = false;
                }
            }
            entity.DllFileName = dr["DllFileName"].ToString();
            entity.DllClassName = dr["DllClassName"].ToString();
            entity.DllMethodName = dr["DllMethodName"].ToString();
            entity.FormName = dr["FormName"].ToString();
            entity.OpenWindowType = dr["OpenWindowType"].ToString();
           
            value = dr["MouseIsClick"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.MouseIsClick = true;
                }
                else
                {
                    entity.MouseIsClick = false;
                }
            }
            value = dr["ImageIndex"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.ImageIndex = Convert.ToInt32(value);
            }
           
            return entity;
        }

        #endregion

        #region  构造函数
        public FrmworkTreeData()
        { }
        public static FrmworkTreeData GetInstance()
        {
            return new FrmworkTreeData();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(FrmworkTreeEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(FrmworkTreeEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string treeId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, treeId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="treeId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string treeId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, treeId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="TreeId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetFrmworkTreeTable(string treeId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, treeId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="treeId">主键Id</param>
        /// <returns>DataTable</returns>
        public FrmworkTreeEntity GetFrmworkTreeEntity(string treeId)
        {
            DataTable dt = GetFrmworkTreeTable(treeId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetFrmworkTreeEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法
        /// <summary>
        /// 根据父节点id获得子节点列表
        /// </summary>
        /// <param name="fatherId">父节点Id</param>
        /// <returns>子节点的table</returns>
        public  DataTable GetChildNodes(string fatherId)
        {
            try
            {
                string tmpStr = "select * from HF_FrmworkTree where   FatherId=@FatherId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@FatherId", fatherId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 根据父节点id获得子节点列表
        /// </summary>
        /// <param name="fatherId">父节点Id</param>
        /// <returns>子节点的Entitys</returns>
        public List<FrmworkTreeEntity>  GetChildNodeEntitys(string fatherId)
        {
            try
            {
                List<FrmworkTreeEntity> result = new List<FrmworkTreeEntity>();
                DataTable dt = GetChildNodes(fatherId);
                foreach (DataRow dr in dt.Rows)
                {
                    FrmworkTreeEntity entity=GetFrmworkTreeEntity(dr);
                    result.Add(entity);
                }
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion
        #endregion
    }
}

